package org.chen.wordwave.act;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;

import org.chen.wordwave.R;
import org.chen.wordwave.base.BaseActivity;
import org.chen.wordwave.tools.db.DBHelper;
import org.chen.wordwave.tools.db.DBStatistics;

import java.util.ArrayList;
import java.util.List;

import lecho.lib.hellocharts.gesture.ZoomType;
import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Column;
import lecho.lib.hellocharts.model.ColumnChartData;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PieChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.SliceValue;
import lecho.lib.hellocharts.model.SubcolumnValue;
import lecho.lib.hellocharts.model.ValueShape;
import lecho.lib.hellocharts.model.Viewport;
import lecho.lib.hellocharts.view.ColumnChartView;
import lecho.lib.hellocharts.view.LineChartView;
import lecho.lib.hellocharts.view.PieChartView;

public class Statistic extends BaseActivity {
    private Context context;
    private PieChartView pieChart;
    private ColumnChartView columnChartView;
    private LineChartView lineChartView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public Integer getLayoutResId() {
        return R.layout.statistic_fragment;
    }

    @Override
    public void init() {
        context = this;

        pieChart = findViewById(R.id.pie_chart);
        columnChartView = findViewById(R.id.column_chart);
        lineChartView = findViewById(R.id.line_chart);

        initLineChart();
        initColumnChart();
        initPieChart();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        View bottomBar = findViewById(R.id.botton_bar);
        if (bottomBar != null) {
            bottomBar.setVisibility(View.VISIBLE);
        }
    }

    private void initPieChart() {
        PieChartData data = new PieChartData();
        int numValues = 3;
        int[] item = new int[]{DBStatistics.getLearnedStatistic(context),
                DBStatistics.getRestWordNum(context),
                DBStatistics.getTotalWordNum(context) - DBStatistics.getLearnedStatistic(context)};
        int[] color = new int[]{getResources().getColor(R.color.pie_color2),
                getResources().getColor(R.color.pie_color3),
                getResources().getColor(R.color.pie_color1)};
        List<SliceValue> values = new ArrayList<>();
        for (int i = 0; i < numValues; ++i) {
            SliceValue sliceValue = new SliceValue(item[i], color[i]);
            values.add(sliceValue);
        }
        data.setValues(values);
        data.setHasLabels(true);
        data.setCenterText1(DBStatistics.getProgressStatistics(context) + "%");
        data.setCenterText1Color(Color.GRAY);
        data.setHasCenterCircle(true);
        data.setSlicesSpacing(24);
        data.setCenterText2(DBHelper.getTitle(context, 1));
        data.setCenterText2Color(Color.GRAY);
        pieChart.setPieChartData(data);
    }

    private void initColumnChart() {
        final String[] cognition = new String[]{getString(R.string.Memorize), getString(R.string.Know), getString(R.string.Vague), getString(R.string.Forget), getString(R.string.Collection), getString(R.string.remaining_today)};
        int[] color = new int[]{Color.parseColor("#298A08"),
                Color.parseColor("#01DFA5"),
                Color.parseColor("#D7DF01"),
                Color.parseColor("#FA5858"),
                Color.parseColor("#673AB7"),
                Color.GRAY
        };
        int[] score = {DBStatistics.getMemorizingStatistic(context),
                DBStatistics.getKnowingStatistic(context),
                DBStatistics.getVaguenessStatistic(context),
                DBStatistics.getForgettingStatistic(context),
                DBStatistics.getFavoritesWordNum(context),
                DBStatistics.getRestWordNum(context)
        };

        ColumnChartData columnData;
        List<AxisValue> axisValues = new ArrayList<>();
        List<Column> columns = new ArrayList<>();
        for (int i = 0; i < cognition.length; ++i) {
            axisValues.add(new AxisValue(i).setLabel(cognition[i]));
            List<SubcolumnValue> values = new ArrayList<>();
            SubcolumnValue subcolumnValue = new SubcolumnValue();
            subcolumnValue.setValue(score[i]);
            subcolumnValue.setColor(color[i]);
            values.add(subcolumnValue);
            Column column = new Column();
            column.setValues(values);
            column.setHasLabels(true);
            columns.add(column);
        }
        columnData = new ColumnChartData(columns);
        Axis axisX = new Axis();
        axisX.setValues(axisValues);
        axisX.setHasLines(true);
        axisX.setTextColor(getResources().getColor(R.color.black));
        axisX.setName(getString(R.string.Familiarity));
        columnData.setAxisXBottom(axisX);
        Axis axisY = new Axis();
        axisY.setHasLines(true);
        axisY.setName(getString(R.string.number_of_words));
        axisY.setTextColor(getResources().getColor(R.color.black));
        columnData.setAxisYLeft(axisY);
        columnChartView.setColumnChartData(columnData);
        columnChartView.setValueSelectionEnabled(true);
        columnChartView.setZoomType(ZoomType.HORIZONTAL);
        columnChartView.setInteractive(true); // Interactive
    }

    private void initLineChart() {
        String[] date = {getString(R.string.today), getString(R.string.Tomorrow), getString(R.string.the_day_after_tomorrow), getString(R.string._4), getString(R.string._5), getString(R.string._6), getString(R.string._7), getString(R.string._8), getString(R.string._9), getString(R.string._10),
                getString(R.string._11), getString(R.string._12), getString(R.string._13), getString(R.string._14), getString(R.string._15), getString(R.string._16), getString(R.string._17), getString(R.string._18), getString(R.string._19), getString(R.string._20), getString(R.string._21),
                getString(R.string.one_month), getString(R.string._2_MONTHS), getString(R.string._3), getString(R.string._6_month), getString(R.string.a_year)};
        float[] score = {100, 33.7f, 27.8f, 26, 25, 25, 25, 24, 24, 24,
                24, 24, 23, 23, 23, 23, 23, 23, 23, 22,
                22, 21, 17, 16, 13, 5};
        List<PointValue> mPointValues = new ArrayList<>();
        List<AxisValue> mAxisXValues = new ArrayList<>();
        for (int i = 0; i < date.length; i++) {
            mAxisXValues.add(new AxisValue(i).setLabel(date[i]));
            mPointValues.add(new PointValue(i, score[i]));
        }
        Line line = new Line(mPointValues).setColor(getResources().getColor(R.color.line_color)); // Line color
        List<Line> lines = new ArrayList<>();
        line.setShape(ValueShape.CIRCLE); // Shape of the points
        line.setCubic(false); // Smooth curve
        line.setStrokeWidth(2); // Line width
        line.setFilled(false); // Fill area
        line.setHasLabels(true); // Labels
        line.setHasLines(true); // Show lines
        line.setHasPoints(true); // Show points
        lines.add(line);
        LineChartData data = new LineChartData();
        data.setLines(lines);
        Axis axisX = new Axis(); // X axis
        axisX.setHasTiltedLabels(true); // Tilted labels
        axisX.setTextColor(getResources().getColor(R.color.black)); // Label color
        axisX.setName(getString(R.string.time));
        axisX.setTextSize(11); // Label size
        axisX.setValues(mAxisXValues); // Set values
        data.setAxisXBottom(axisX); // X at bottom
        axisX.setHasLines(true); // X axis lines
        Axis axisY = new Axis(); // Y axis
        axisY.setName(getString(R.string.ForgettingRate)); // Y axis label
        axisY.setTextColor(getResources().getColor(R.color.black));
        axisY.setTextSize(11); // Label size
        data.setAxisYLeft(axisY); // Y on left
        lineChartView.setInteractive(true);
        lineChartView.setZoomType(ZoomType.HORIZONTAL); // Zoom type
        lineChartView.setMaxZoom((float) 3); // Max zoom
        lineChartView.setLineChartData(data);
        lineChartView.setVisibility(View.VISIBLE);
        Viewport viewport = new Viewport(lineChartView.getMaximumViewport());
        viewport.left = 0;
        viewport.right = 7;
        lineChartView.setCurrentViewport(viewport);
    }
}
